<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\home\model\page\circle;

use app\api\model\dao\Circle;
use app\api\model\dao\UpRank;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Log;
use think\Validate;
use think\Exception;

class ApproveCircle extends PageBase
{

    private $objCircle;
    private $objUpRank;

    public function __construct()
    {
        parent::__construct();

        $this->objCircle = new Circle();
        $this->objUpRank = new UpRank();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'cir_id' => 'require|number',
            'status' => 'require|number|in:1,2',
        ];
        $msg = [
            'cir_id.require' => '圈子id不能为空',
            'status.require' => '圈子审核状态不能为空',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
//        $ret = $this->checkToken($param);
//        if (false === $ret) {
//            return $this->getRes(Error::ERR_SYS);
//        }

        // 1、参数校验
        $ret = $this->checkParam($param);

        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }


        // 2、从数据库中获取圈子详情
        $ret = $this->getCircleInfoFromDB($param['cir_id']);
        if (false === $ret) {
            //throw new Exception('get circle info from db fail. [circle_id: ' . $param['cir_id'] . ']', Error::ERR_SYS);
            return $this->getRes(Error::ERR_SYS, Error::$arr_err_msg[Error::ERR_SYS] . 'get circle info from db fail');
        }

        if (empty($ret)) {
            //throw new Exception('circle is empty. [circle_id: ' . $param['cir_id'] . ']', Error::ERR_CIRCLE_NOT_EXIST);
            return $this->getRes(Error::ERR_CIRCLE_NOT_EXIST);
        }

        // 3、校验是否被删除
        if (Flag::CONST_CIRCLE_DELETED == $ret['is_delete']) {
            return $this->getRes(Error::ERR_CIRCLE_DELETED);
        }

        // 4、校验是否审核
        if (Flag::CONST_CIRCLE_APPROVE_PASS == $ret['approve_status']) {
            return $this->getRes(Error::ERR_CIRCLE_APPROVE_PASS);
        }

        if (Flag::CONST_CIRCLE_APPROVE_REJECT == $ret['approve_status']) {
            return $this->getRes(Error::ERR_CIRCLE_APPROVE_REJECT);
        }

        $ret = $this->objCircle->approveCircle($param);
        if (false === $ret) {
            //throw new Exception('data is exist', Error::ERR_SYS);
            return $this->getRes(Error::ERR_SYS);
        }

        //
        if (Flag::CONST_CIRCLE_APPROVE_PASS == $param['status']) {
            $up_rank_ret = $this->objUpRank->plusUp($ret['creator'], date('Ymd'));
            if (false === $up_rank_ret) {
                Log::error(' add consumer rank fail. [userid:' . $ret['creator'] . '][date:' . date('Ymd') . ']');
            }
        }

        return $this->getRes();

    }

    /**
     * 从数据库中获取圈子详情
     *
     * @param $cir_id
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getCircleInfoFromDB($cir_id)
    {
        if (!$cir_id) {
            return false;
        }
        $arrCond = [
            'id' => ['=', $cir_id]
        ];
        $ret = $this->objCircle->getCircle($arrCond);
        if (false === $ret) {
            return false;
        }
        $ret = $ret[0];
        return $ret;
    }
}
